package com.example.moshudriver.model;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.BeeFramework.model.BaseModel;
import com.BeeFramework.model.BeeCallback;
import com.example.moshudriver.protocol.ApiInterface;
import com.example.moshudriver.protocol.CONFIG;
import com.example.moshudriver.protocol.LOCATION;
import com.example.moshudriver.protocol.MY_SERVICE;
import com.example.moshudriver.protocol.USER;
import com.example.moshudriver.protocol.location_chargeRequest;
import com.example.moshudriver.protocol.location_chargeResponse;
import com.example.moshudriver.protocol.myservicelistRequest;
import com.example.moshudriver.protocol.myservicelistResponse;
import com.example.moshudriver.protocol.pushswitchRequest;
import com.example.moshudriver.protocol.pushswitchResponse;
import com.example.moshudriver.protocol.userbalanceRequest;
import com.example.moshudriver.protocol.userbalanceResponse;
import com.example.moshudriver.protocol.userchange_avatarRequest;
import com.example.moshudriver.protocol.userchange_avatarResponse;
import com.example.moshudriver.protocol.userprofileRequest;
import com.example.moshudriver.protocol.userprofileResponse;
import com.example.moshudriver.protocol.usersigninRequest;
import com.example.moshudriver.protocol.usersigninResponse;
import com.example.moshudriver.protocol.usersignoutRequest;
import com.example.moshudriver.protocol.usersignoutResponse;
import com.example.moshudriver.protocol.withdrawmoneyRequest;
import com.example.moshudriver.protocol.withdrawmoneyResponse;
import com.example.moshudriver.tools.LocationManager;
import com.example.moshudriver.tools.O2OMobile;
import com.example.moshudriver.tools.O2OMobileAppConst;
import com.example.moshudriver.tools.SESSION;
import com.external.androidquery.callback.AjaxStatus;

public class UserBalanceModel extends BaseModel {

    public String publicBalance;

    public UserBalanceModel(Context context) {
        super(context);
    }

    public List<MY_SERVICE> publicMyServiceList = new ArrayList<MY_SERVICE>();
    private int NUMPERPAGE = 20;
    public int publicMore;
    public USER publicUser;
    public pushswitchResponse publicPushSwitchResponse;
    
    
    //更新位置
	public void RefreshLocation() {

		usersigninRequest request = new usersigninRequest();
		
		request.sid = SESSION.getInstance().sid;
		request.uid = SESSION.getInstance().uid;
	    request.ver = O2OMobileAppConst.VERSION_CODE;
		LOCATION location = new LOCATION();
		location.lat = LocationManager.getLatitude();
		location.lon = LocationManager.getLongitude();
		request.location = location;
		BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {
			@Override
			public void callback(String url, JSONObject jo, AjaxStatus status) {

				// 获得网络的json数据之后，的回调函数
				try {
					UserBalanceModel.this.callback(this, url, jo, status);

					if (null != jo) {
						// 用户成功登录之后，返回的数据，转换为usersigninResponse对象
						usersigninResponse usersigninResponse = new usersigninResponse();
						usersigninResponse.fromJson(jo);
						if (usersigninResponse.succeed == 1) {
							// 保存返回来的数据
						

							// 回调到登录界面
							UserBalanceModel.this.OnMessageResponse(url, jo, status);
						} else {
//							UserBalanceModel.this.callback(url, usersigninResponse.error_code, usersigninResponse.error_desc);
						}

					}

				} catch (JSONException e) {

				}
				;
			}
		};
		Map<String, String> params = new HashMap<String, String>();
		try {
			params.put("json", request.toJson().toString());
		} catch (JSONException e) {

		}
		// 参数(json类型)和接口
		cb.url(ApiInterface.LOCATION_REFRESH).type(JSONObject.class).params(params);
		// 提交参数，并且访问服务器
		ajax(cb);
	}
    
    
    
    

    public void get() {
        userbalanceRequest request = new userbalanceRequest();
        request.sid = SESSION.getInstance().sid;
        request.uid = SESSION.getInstance().uid;
        request.ver = O2OMobileAppConst.VERSION_CODE;

        BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

            @Override
            public void callback(String url, JSONObject jo, AjaxStatus status) {
                try {
                    UserBalanceModel.this.callback(this, url, jo, status);
                    if (null != jo) {
                        userbalanceResponse response = new userbalanceResponse();
                        response.fromJson(jo);
                        if (response.succeed == 1) {
                            publicBalance = response.balance;
                            UserBalanceModel.this.OnMessageResponse(url, jo, status);
                        } else {
                            UserBalanceModel.this.callback(url, response.error_code, response.error_desc);
                        }
                    }
                } catch (JSONException e) {

                }
            }
        };

        Map<String, Object> params = new HashMap<String, Object>();
        try {
            params.put("json", request.toJson().toString());
        } catch (JSONException e) {

        }
        cb.url(ApiInterface.USER_BALANCE).type(JSONObject.class).params(params);
        ajax(cb);
    }
    
//    充值
    public void InMoney(String amount) {
        withdrawmoneyRequest request = new withdrawmoneyRequest();
        request.sid = SESSION.getInstance().sid;
        request.uid = SESSION.getInstance().uid;
        request.ver = O2OMobileAppConst.VERSION_CODE;
        request.amount = amount;
        BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

            @Override
            public void callback(String url, JSONObject jo, AjaxStatus status) {
                try {
                    UserBalanceModel.this.callback(this, url, jo, status);
                    if (null != jo) {
                        withdrawmoneyResponse response = new withdrawmoneyResponse();
                        response.fromJson(jo);
                        if (response.succeed == 1) {
                            UserBalanceModel.this.OnMessageResponse(url, jo, status);
                        } else {
                            UserBalanceModel.this.callback(url, response.error_code, response.error_desc);
                        }
                    }
                } catch (JSONException e) {

                }
            }
        };

        Map<String, Object> params = new HashMap<String, Object>();
        try {
            params.put("json", request.toJson().toString());
        } catch (JSONException e) {

        }
        cb.url(ApiInterface.WITHDRAW_INMONEY).type(JSONObject.class).params(params);
        ajax(cb);
    }
    
  //提现
    public void withdrawMoney(String amount,String alipay,String realname,String pwd) {
        withdrawmoneyRequest request = new withdrawmoneyRequest();
        request.sid = SESSION.getInstance().sid;
        request.uid = SESSION.getInstance().uid;
        request.ver = O2OMobileAppConst.VERSION_CODE;
        request.amount = amount;
        request.alipay_account=alipay;
        request.real_name=realname;
        request.pwd=pwd;
        BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

            @Override
            public void callback(String url, JSONObject jo, AjaxStatus status) {
                try {
                    UserBalanceModel.this.callback(this, url, jo, status);
                    if (null != jo) {
                        withdrawmoneyResponse response = new withdrawmoneyResponse();
                        response.fromJson(jo);
                        if (response.succeed == 1) {
                            UserBalanceModel.this.OnMessageResponse(url, jo, status);
                        } else {
                            UserBalanceModel.this.callback(url, response.error_code, response.error_desc);
                        }
                    }
                } catch (JSONException e) {

                }
            }
        };

        Map<String, Object> params = new HashMap<String, Object>();
        try {
            params.put("json", request.toJson().toString());
        } catch (JSONException e) {

        }
        cb.url(ApiInterface.WITHDRAW_MONEY).type(JSONObject.class).params(params);
        ajax(cb);
    }
    public void getServiceList(int user) {
        myservicelistRequest request = new myservicelistRequest();
        request.sid = SESSION.getInstance().sid;
        request.uid = SESSION.getInstance().uid;
        request.ver = O2OMobileAppConst.VERSION_CODE;
        request.user = user;
        request.by_no = 1;
        request.count = NUMPERPAGE;
        BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

            @Override
            public void callback(String url, JSONObject jo, AjaxStatus status) {
                try {
                    UserBalanceModel.this.callback(this, url, jo, status);
                    if (null != jo) {
                        myservicelistResponse response = new myservicelistResponse();
                        response.fromJson(jo);
                        publicMore = response.more;
                        if (response.succeed == 1) {
                            publicMyServiceList.clear();
                            publicMyServiceList.addAll(response.services);
                            UserBalanceModel.this.OnMessageResponse(url, jo, status);
                        } else {
                            UserBalanceModel.this.callback(url, response.error_code, response.error_desc);
                        }
                    } else {
                        UserBalanceModel.this.OnMessageResponse(url, jo, status);
                    }
                } catch (JSONException e) {

                }
            }
        };

        Map<String, Object> params = new HashMap<String, Object>();
        try {
            params.put("json", request.toJson().toString());
        } catch (JSONException e) {

        }
        cb.url(ApiInterface.MYSERVICE_LIST).type(JSONObject.class).params(params);
        ajax(cb);
    }

    public void getProfile(final int user) {
        userprofileRequest request = new userprofileRequest();
        request.sid = SESSION.getInstance().sid;
        request.uid = SESSION.getInstance().uid;
        request.ver = O2OMobileAppConst.VERSION_CODE;
        request.user = user;
        BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

            @Override
            public void callback(String url, JSONObject jo, AjaxStatus status) {
                try {
                    UserBalanceModel.this.callback(this, url, jo, status);
                    if (null != jo) {
                        userprofileResponse response = new userprofileResponse();
                        response.fromJson(jo);
                        if (response.succeed == 1) {
                            if (response.user.id == SESSION.getInstance().uid) {
                                editor.putString("user", response.user.toJson().toString());
                                editor.commit();
                            }
                            UserBalanceModel.this.OnMessageResponse(url, jo, status);
                        } else {
                            UserBalanceModel.this.callback(url, response.error_code, response.error_desc);
                        }
                    }
                } catch (JSONException e) {

                }
            }
        };

        Map<String, Object> params = new HashMap<String, Object>();
        try {
            params.put("json", request.toJson().toString());
        } catch (JSONException e) {

        }
        cb.url(ApiInterface.USER_PROFILE).type(JSONObject.class).params(params);
        ajaxProgress(cb);
    }

    public void getServiceListMore(int user) {
        myservicelistRequest request = new myservicelistRequest();
        request.sid = SESSION.getInstance().sid;
        request.uid = SESSION.getInstance().uid;
        request.ver = O2OMobileAppConst.VERSION_CODE;
        request.user = user;
        request.by_no = (int) Math.ceil(publicMyServiceList.size() * 1.0 / NUMPERPAGE) + 1;
        ;
        request.count = NUMPERPAGE;
        BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

            @Override
            public void callback(String url, JSONObject jo, AjaxStatus status) {
                try {
                    UserBalanceModel.this.callback(this, url, jo, status);
                    if (null != jo) {
                        myservicelistResponse response = new myservicelistResponse();
                        response.fromJson(jo);
                        publicMore = response.more;
                        if (response.succeed == 1) {
                            publicMyServiceList.addAll(response.services);
                            UserBalanceModel.this.OnMessageResponse(url, jo, status);
                        } else {
                            UserBalanceModel.this.callback(url, response.error_code, response.error_desc);
                        }
                    }
                } catch (JSONException e) {

                }
            }
        };

        Map<String, Object> params = new HashMap<String, Object>();
        try {
            params.put("json", request.toJson().toString());
        } catch (JSONException e) {

        }
        cb.url(ApiInterface.MYSERVICE_LIST).type(JSONObject.class).params(params);
        ajax(cb);
    }

    public void changeAvatar(File avatar) {
        userchange_avatarRequest request = new userchange_avatarRequest();
        request.uid = SESSION.getInstance().uid;
        request.sid = SESSION.getInstance().sid;
        request.ver = O2OMobileAppConst.VERSION_CODE;

        BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

            @Override
            public void callback(String url, JSONObject jo, AjaxStatus status) {
                try {
                    UserBalanceModel.this.callback(this, url, jo, status);
                    if (null != jo) {
                        userchange_avatarResponse response = new userchange_avatarResponse();
                        response.fromJson(jo);
                        if (response.succeed == 1) {
                            publicUser = response.user;
                            editor.putString("user", publicUser.toJson().toString());
                            editor.commit();
                            UserBalanceModel.this.OnMessageResponse(url, jo, status);
                        } else {
                            UserBalanceModel.this.callback(url, response.error_code, response.error_desc);
                        }
                    }

                } catch (JSONException e) {

                }
            }
        };

        Map<String, Object> params = new HashMap<String, Object>();
        try {
            params.put("json", request.toJson().toString());
            params.put("avatar", avatar);
        } catch (JSONException e) {

        }
        cb.url(ApiInterface.USER_CHANGE_AVATAR).type(JSONObject.class).params(params);
        ajaxProgress(cb);
    }

    public void pushSwitch(int push_switch) {
        pushswitchRequest request = new pushswitchRequest();
        request.push_switch = push_switch;
        request.uid = SESSION.getInstance().uid;
        request.sid = SESSION.getInstance().sid;
        request.UUID = O2OMobile.getDeviceId(mContext);
        request.ver = O2OMobileAppConst.VERSION_CODE;
        BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {
            @Override
            public void callback(String url, JSONObject jo, AjaxStatus status) {
                try {
                    UserBalanceModel.this.callback(this, url, jo, status);
                    if (null != jo) {
                        publicPushSwitchResponse = new pushswitchResponse();
                        publicPushSwitchResponse.fromJson(jo);
                        if (publicPushSwitchResponse.succeed == 1) {
                            CONFIG config = publicPushSwitchResponse.config;
                            int push = config.push;
                            if (push == 1) {
                                editor.putBoolean("push", true);
                            } else {
                                editor.putBoolean("push", false);
                            }
                            editor.commit();
                            UserBalanceModel.this.OnMessageResponse(url, jo, status);
                        } else {
                            UserBalanceModel.this.callback(url, publicPushSwitchResponse.error_code, publicPushSwitchResponse.error_desc);
                        }
                    }

                } catch (JSONException e) {

                }
                ;
            }
        };
        Map<String, String> params = new HashMap<String, String>();
        try {
            params.put("json", request.toJson().toString());
        } catch (JSONException e) {

        }

        cb.url(ApiInterface.PUSH_SWITCH).type(JSONObject.class).params(params);
        ajaxProgress(cb);
    }
    //根据司机id获取相关信息
    public void getProfile_driver() {
        userprofileRequest request = new userprofileRequest();
        request.sid = SESSION.getInstance().sid;
        request.uid = SESSION.getInstance().uid;
        request.ver = O2OMobileAppConst.VERSION_CODE;
        request.user = SESSION.getInstance().uid;
        BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

            @Override
            public void callback(String url, JSONObject jo, AjaxStatus status) {
                try {
                    UserBalanceModel.this.callback(this, url, jo, status);
                    if (null != jo) {
                        userprofileResponse response = new userprofileResponse();
                        response.fromJson(jo);
                        if (response.succeed == 1) {
                            if (response.user.id == SESSION.getInstance().uid) {
                                editor.putString("user", response.user.toJson().toString());
                                editor.commit();
                            }
                            UserBalanceModel.this.OnMessageResponse(url, jo, status);
                        } else {
                            UserBalanceModel.this.callback(url, response.error_code, response.error_desc);
                        }
                    }
                } catch (JSONException e) {

                }
            }
        };

        Map<String, Object> params = new HashMap<String, Object>();
        try {
            params.put("json", request.toJson().toString());
        } catch (JSONException e) {

        }
        cb.url(ApiInterface.USER_PROFILE_DRIVER).type(JSONObject.class).params(params);
        ajax(cb);
    }
    public void signout() {

        usersignoutRequest request = new usersignoutRequest();
        request.uid = SESSION.getInstance().uid;
        request.sid = SESSION.getInstance().sid;
        request.ver = O2OMobileAppConst.VERSION_CODE;
        BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {
            @Override
            public void callback(String url, JSONObject jo, AjaxStatus status) {
                try {
                    UserBalanceModel.this.callback(this, url, jo, status);
                    if (null != jo) {
                        usersignoutResponse response = new usersignoutResponse();
                        response.fromJson(jo);
                        if (response.succeed == 1) {
                        } else {
                            UserBalanceModel.this.callback(url, response.error_code, response.error_desc);
                        }
                    }
                    UserBalanceModel.this.OnMessageResponse(url, jo, status);

                } catch (JSONException e) {

                }
            }
        };
        Map<String, String> params = new HashMap<String, String>();
        try {
            params.put("json", request.toJson().toString());
        } catch (JSONException e) {

        }

        cb.url(ApiInterface.USER_SIGNOUT).type(JSONObject.class).params(params);
        ajaxProgress(cb);
    }
    public void getLoccation_Charge(String name) {
    	location_chargeRequest request = new location_chargeRequest();
        request.sid = SESSION.getInstance().sid;
        request.uid = SESSION.getInstance().uid;
        request.region_name=name;
        request.ver = O2OMobileAppConst.VERSION_CODE;

        BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

            @Override
            public void callback(String url, JSONObject jo, AjaxStatus status) {
                try {
                    UserBalanceModel.this.callback(this, url, jo, status);
                    
                  
                    
                    if (null != jo) {
                        location_chargeResponse response = new location_chargeResponse();
                        response.fromJson(jo);
                        if (response.succeed == 1) {
//                            publicBalance = response.balance;
                            UserBalanceModel.this.OnMessageResponse(url, jo, status);
                        } else {
                            UserBalanceModel.this.callback(url, response.error_code, response.error_desc);
                        }
                    }
                } catch (Exception e) {

                }
            }
        };

        Map<String, Object> params = new HashMap<String, Object>();
        try {
            params.put("json", request.toJson().toString());
        } catch (JSONException e) {

        }
        cb.url(ApiInterface.USER_LOCATION_CHARGE).type(JSONObject.class).params(params);
        ajax(cb);
    }
    
  //设置新的支付密码
    public void SetNewPwd(String pwd) {
        withdrawmoneyRequest request = new withdrawmoneyRequest();
        request.sid = SESSION.getInstance().sid;
        request.uid = SESSION.getInstance().uid;
        request.ver = O2OMobileAppConst.VERSION_CODE;
        request.pwd = pwd;
        BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

            @Override
            public void callback(String url, JSONObject jo, AjaxStatus status) {
                try {
                    UserBalanceModel.this.callback(this, url, jo, status);
                    if (null != jo) {
                        withdrawmoneyResponse response = new withdrawmoneyResponse();
                        response.fromJson(jo);
                        if (response.succeed == 1) {
                            UserBalanceModel.this.OnMessageResponse(url, jo, status);
                        } else {
                            UserBalanceModel.this.callback(url, response.error_code, response.error_desc);
                        }
                    }
                } catch (JSONException e) {

                }
            }
        };

        Map<String, Object> params = new HashMap<String, Object>();
        try {
            params.put("json", request.toJson().toString());
        } catch (JSONException e) {

        }
        cb.url(ApiInterface.WITHDRAW_SET_PWD).type(JSONObject.class).params(params);
        ajax(cb);
    }
     
    
  //是否设置了提现密码
    public void isPassword() {
        withdrawmoneyRequest request = new withdrawmoneyRequest();
        request.sid = SESSION.getInstance().sid;
        request.uid = SESSION.getInstance().uid;
        request.ver = O2OMobileAppConst.VERSION_CODE;
        BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

            @Override
            public void callback(String url, JSONObject jo, AjaxStatus status) {
                try {
                    UserBalanceModel.this.callback(this, url, jo, status);
                    if (null != jo) {
                        withdrawmoneyResponse response = new withdrawmoneyResponse();
                        response.fromJson(jo);
                        if (response.succeed == 1) {
                            UserBalanceModel.this.OnMessageResponse(url, jo, status);
                        } else {
                            UserBalanceModel.this.callback(url, response.error_code, response.error_desc);
                        }
                    }
                } catch (JSONException e) {

                }
            }
        };

        Map<String, Object> params = new HashMap<String, Object>();
        try {
            params.put("json", request.toJson().toString());
        } catch (JSONException e) {

        }
        cb.url(ApiInterface.WITHDRAW_IS_PWD).type(JSONObject.class).params(params);
        ajax(cb);
    }
}
